/*
 * @Description: 请描述此文件
 * @Author: LDM
 * @Date: 2021-09-03 14:25:09
 * @LastEditors: LDM
 * @LastEditTime: 2021-09-30 17:12:12
 */
import type { RouteRecordRaw } from 'vue-router'
// import type { IAppRouteRecordRaw } from './types'
import type { App } from 'vue'

// import { usePermisionnStoreWithOut } from '/@/store'
import { createRouter, createWebHashHistory } from 'vue-router'
import { basicRoutes } from './basicRoutes'
import { importRouterModules } from './helper/indexHelper'
import { setupPermissionGuard } from './guard/permission'

export const otherRoutes = importRouterModules()
// 添加权限路由
// const permissionStore = usePermisionnStoreWithOut()
// permissionStore.SetPermissionRoutes(basicRoutes) // TODO 后面换成basicRoutes

// app router
export const router = createRouter({
  history: createWebHashHistory(),
  routes: basicRoutes as unknown as RouteRecordRaw[], // TODO 后面换成basicRoutes
  strict: true,
  scrollBehavior: () => ({ left: 0, top: 0 })
})
// reset router
export function resetRouter() {
  router.getRoutes().forEach((route) => {
    const { name } = route
    if (otherRoutes.some((item) => item.name === name)) {
      if (name) {
        router.removeRoute(name)
      }
    }
  })
}
// config router
export function setupRouter(app: App<Element>) {
  app.use(router)
}

setupPermissionGuard(router)
